<template>
  <div class="UserLogin">
    <h2 class="Login">用户登录界面</h2>
    <a-form
      :model="form"
      auto-label-width:true
      layout="horizontal"
      style="max-width: 480px; margin: 0 auto"
      @submit="handleSubmit()"
    >
      <a-form-item
        :rules="[{ required: true, message: '用户名不能小于4位' }]"
        field="userAccount"
        label="用户名"
        validate-trigger="input"
      >
        <a-input v-model="form.userAccount" placeholder="请输入用户名" />
        <template #extra>
          <div>用户名</div>
        </template>
      </a-form-item>
      <a-form-item
        :rules="[{ required: true, message: '密码不能小于8位' }]"
        field="userPassword"
        label="密码"
        validate-trigger="input"
      >
        <a-input-password
          v-model="form.userPassword"
          placeholder="请输入密码"
        />
        <template #extra>
          <div>请输入密码</div>
        </template>
      </a-form-item>
      <a-form-item id="foot">
        <a-button html-type="submit" style="width: 120px" type="primary"
          >提交
        </a-button>
      </a-form-item>
    </a-form>
  </div>
</template>

<script lang="ts" setup>
import { reactive } from "vue";
import { UserControllerService, UserLoginRequest } from "../../../generated";
import message from "@arco-design/web-vue/es/message";
import { useStore } from "vuex";
import { useRoute, useRouter } from "vue-router";

const form = reactive({
  userAccount: "",
  userPassword: "",
}) as UserLoginRequest;

const store = useStore();
const router = useRouter();
const route = useRoute();
const redirect =
  route.query.redirect === undefined ? "/" : route.query.redirect;
const handleSubmit = async () => {
  console.log(redirect);
  const res = await UserControllerService.userLoginUsingPost(form);
  console.log(res);
  if (res.code == 0) {
    router.push({
      path: redirect as string,
      replace: true,
    });
    // 获取登录信息
    store.dispatch("user/getLoginUser");
  } else {
    message.error("登录失败" + res.message);
  }
};
</script>

<style scoped>
.UserLogin {
  text-align: center;
}

.Login {
  margin-bottom: 10px;
}

#foot {
  justify-content: center;
}
</style>
